package com.foldingleaf.web.controller.bank;


import com.foldingleaf.bank.service.IUserBankCardService;
import com.foldingleaf.common.annotation.Log;
import com.foldingleaf.common.core.controller.BaseController;
import com.foldingleaf.common.core.domain.Result;
import com.foldingleaf.common.core.page.TableDataInfo;
import com.foldingleaf.common.enums.BusinessType;
import com.foldingleaf.common.utils.poi.ExcelUtil;
import com.foldingleaf.withdraw.domain.UserBankCard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户银行卡绑定Controller
 * 
 * @author ajian
 * @date 2022-06-22
 */
@RestController
@RequestMapping("/bank/bank")
public class UserBankCardController extends BaseController
{
    @Autowired
    private IUserBankCardService userBankCardService;

    /**
     * 查询用户银行卡绑定列表
     */
    @PreAuthorize("@ss.hasPermi('bank:bank:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserBankCard userBankCard)
    {
        startPage();
        List<UserBankCard> list = userBankCardService.selectUserBankCardList(userBankCard);
        return getDataTable(list);
    }

    /**
     * 导出用户银行卡绑定列表
     */
    @PreAuthorize("@ss.hasPermi('bank:bank:export')")
    @Log(title = "用户银行卡绑定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserBankCard userBankCard)
    {
        List<UserBankCard> list = userBankCardService.selectUserBankCardList(userBankCard);
        ExcelUtil<UserBankCard> util = new ExcelUtil<UserBankCard>(UserBankCard.class);
        util.exportExcel(response, list, "用户银行卡绑定数据");
    }

    /**
     * 获取用户银行卡绑定详细信息
     */
    @PreAuthorize("@ss.hasPermi('bank:bank:query')")
    @GetMapping(value = "/{cardId}")
    public Result getInfo(@PathVariable("cardId") Long cardId)
    {
        return Result.success(userBankCardService.selectUserBankCardByCardId(cardId));
    }

    /**
     * 新增用户银行卡绑定
     */

    @Log(title = "用户银行卡绑定", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody UserBankCard userBankCard)
    {
        if (userBankCardService.selectUserBankCardList(userBankCard).size() > 0)
        {
            return Result.error("该用户已经绑定过银行卡，请勿重复绑定");
        }
        return toAjax(userBankCardService.insertUserBankCard(userBankCard));
    }

    /**
     * 修改用户银行卡绑定
     */
    @PreAuthorize("@ss.hasPermi('bank:bank:edit')")
    @Log(title = "用户银行卡绑定", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody UserBankCard userBankCard)
    {
        return toAjax(userBankCardService.updateUserBankCard(userBankCard));
    }

    /**
     * 删除用户银行卡绑定
     */
    @PreAuthorize("@ss.hasPermi('bank:bank:remove')")
    @Log(title = "用户银行卡绑定", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cardIds}")
    public Result remove(@PathVariable Long[] cardIds)
    {
        return toAjax(userBankCardService.deleteUserBankCardByCardIds(cardIds));
    }
}
